import axios from 'axios'

const service = process.env.BASE_URL

axios.interceptors.request.use(
  (config) => {
    if (!config.url.match(/[a-zA-z]+:\/\/[^/]*/)) {
      config.url = `${service}${config.url}`
    }
    return config
  },
  (error) => {
    console.log(error)
    return Promise.reject(error)
  }
)

let isShowReLoginModal = false

axios.interceptors.response.use(
  (response) => {
    if (!isShowReLoginModal && (response.data.code === 405 || response.data.code === 403)) {
      isShowReLoginModal = true
      /* Vue.$Modal.warning({
        content: '您的账号已在别处登录,请确认后重试',
        okText: '点击前往登录页',
        onOk: () => {
          Vue.$router.push('/login')
          window.location.reload()
          isShowReLoginModal = false
        }
      }) */
      const modal = document.querySelectorAll('.ivu-modal')
      const modalStyle = modal[modal.length - 1].style
      modalStyle.display = 'flex'
      modalStyle.top = '50%'
      modalStyle.transform = 'translate(0, -50%)'
    }
    return response
  },
  (error) => {
    console.log(error)
    return Promise.reject(error)
  }
)

export default axios
